package example;

/*$Id: largetable.src,v 1.3 2002/09/09 05:35:19 tonyjpaul Exp $*/
/*
 * @(#)largetable.java
 * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
 * Please read the COPYRIGHTS file for more details.
 */

/** 
 *  An example of using the SnmpTablePanel bean which is designed for large
 *  table support.
 *<img SRC="images/v3only.jpg" ALT="v3 only"> [-v] <version>      - version(v1 / v2 / v3). By default v1.
 * [-c] <community>    - community String. By default "public".
 * [-p] <port>         - remote port no. By default 161.
 * [-r] <Retries>      - Retries. By default 0.      
 * [-t] <Timeout>      - Timeout. By default 5000ms.
 *<img SRC="images/v3only.jpg" ALT="v3 only"> [-u] <username>     - The v3 principal/userName
 * [-a] <autProtocol>  - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
 * [-w] <authPassword> - The authentication password.
 * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
 * [-n] <contextName>  - The snmpv3 contextName to be used.
 * [-i] <contextID>    - The snmpv3 contextID to be used.
 * [-m] <mibs>  Mandatory - The mibs to be loaded.
 * host Mandatory      - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
 * tableOID  Mandatory - Give the Object Identifier of a Table.
 **/

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

import com.adventnet.snmp.beans.SnmpTarget;
import com.adventnet.snmp.ui.SnmpTablePanel;
import com.adventnet.tools.utils.ParseOptions;

public class largetable extends JFrame {    
    


    private static final int VERSION = 0;
    private static final int COMMUNITY = 1;
    private static final int PORT = 2;
    private static final int RETRIES = 3;
    private static final int TIMEOUT = 4;
    private static final int USER_NAME = 5;
    private static final int AUTH_PROTOCOL = 6;
    private static final int AUTH_PASSWORD = 7;
    private static final int PRIV_PASSWORD = 8;
    private static final int CONTEXTNAME = 9;
    private static final int CONTEXTID = 10;
    
    SnmpTablePanel tablePanel;
    


 /** <img SRC="images/v3only.jpg" ALT="v3 only"> Supply command line arguments to this constructor **/
    public largetable(String host, String community, String mibs,
                    String tableOID, int port, int retries, int timeout) {
      
             this("v1", host, community, mibs, tableOID, port, retries, timeout, "", "", "", "","","");

  } /*<img SRC="images/v3only.jpg" ALT="v3 only"> largetable */
  

 /** <img SRC="images/v3only.jpg" ALT="v3 only"> Supply command line arguments to this constructor **/
    public largetable(String version, String host, String community, String mibs, String tableOID, int port, int retries,int timeout, String userName, String authProtocol, String authPassword, String privPassword,String contextName,String contextID) {
                                 

    super("SnmpTablePanel");
    tablePanel = new SnmpTablePanel();
    tablePanel.setTargetHost (host);
    tablePanel.setCommunity (community);
    tablePanel.setTargetPort (port);
    tablePanel.setRetries (retries);
    tablePanel.setTimeout (timeout);

	 //To load MIBs from compiled file
	 tablePanel.setLoadFromCompiledMibs(true);


    if(version.equals("v3"))
        tablePanel.setSnmpVersion(SnmpTarget.VERSION3);
    else if(version.equals("v2"))
        tablePanel.setSnmpVersion(SnmpTarget.VERSION2C);
    else tablePanel.setSnmpVersion(SnmpTarget.VERSION1);

    if(version.equals("v3")){
        tablePanel.setPrincipal(userName);

        if(authProtocol.compareTo("MD5") == 0)
                tablePanel.setAuthProtocol(SnmpTarget.MD5_AUTH);
        else if(authProtocol.compareTo("SHA") == 0)
                tablePanel.setAuthProtocol(SnmpTarget.SHA_AUTH);
           else tablePanel.setAuthProtocol(SnmpTarget.NO_AUTH);

        tablePanel.setAuthPassword(authPassword);
           tablePanel.setPrivPassword(privPassword);
           tablePanel.setContextName(contextName);
           tablePanel.setContextID(contextID);
       }

    try {
        System.out.println("Loading MIBs: "+mibs);
        tablePanel.loadMibs(mibs);
        System.out.println("Done.");
		if(tablePanel.getSnmpVersion() == SnmpTarget.VERSION3){
			tablePanel.create_v3_tables();
		}
        tablePanel.setTableOID (tableOID);

    } catch (Exception ex) {
        System.err.println("Error: "+ex);
        System.exit(1);
    }
    getContentPane().add(tablePanel);
    setVisible(true);
    show();    
    }
    
    
  /**  The main method to start the program **/
    public static void main(String[] args) {

    // Take care of getting options
        String usage = "largetable [-v version(v1/v2/v3)] [-c community] [-p port] [-r retries] [-t timeout] [-u userName] [-a authProtocol] [-w authPassword] [-s privPassword] [-n contextname] [-i contextID] host tableOID MIB_files ";
        String options[] = { "-v", "-c", "-p", "-r", "-t", "-u", "-a", "-w", "-s","-n","-i" };
        String values[] = { null, null, null, null, null, null, null, null, null, null,null};

    ParseOptions opt = new ParseOptions(args,options,values, usage);

    // check for at least hostname and one OID in remaining arguments
    if (opt.remArgs.length<3) opt.usage_error();

    String host = opt.remArgs[0];
    String oidTable = opt.remArgs[1];
    String community = "public";
    String mibs = opt.remArgs[2];


    String version ="v1";
    String userName = "";
    String authProtocol = new String("NO_AUTH");
    String authPassword = new String ("");
    String privPassword = new String ("");
	String contextName = new String (""); 
	String contextID = new String (""); 

    if (values[COMMUNITY] != null) community = values[COMMUNITY];

    int port = 161, retries=0,timeout = 1;
    try {
        if (values[PORT] != null) 
            port = ( Integer.parseInt(values[PORT]) );
        if (values[RETRIES] != null) 
            retries = ( Integer.parseInt(values[RETRIES]) );
        if (values[TIMEOUT] != null) 
            timeout = ( Integer.parseInt(values[TIMEOUT]) );
    } catch (NumberFormatException ex) {
        System.err.println("Invalid Integer Argument "+ex);
    }

    if(values[VERSION] != null)
        version = values[VERSION];

        if((version.equals("v3")) && (values[USER_NAME] == null)) opt.usage_error();
    if(values[USER_NAME] != null)
        userName = values[USER_NAME];
    if(values[AUTH_PROTOCOL] != null)
        authProtocol = values[AUTH_PROTOCOL];
    if(values[AUTH_PASSWORD] != null)
        authPassword = values[AUTH_PASSWORD];
    if(values[PRIV_PASSWORD] != null)
        privPassword = values[PRIV_PASSWORD];
    if(values[CONTEXTNAME] != null)
        contextName = values[CONTEXTNAME];
    if(values[CONTEXTID] != null)
        contextID = values[CONTEXTID];



      JFrame frame = new largetable(version, host, community, mibs, oidTable, port, retries, timeout,
          userName, authProtocol, authPassword, privPassword,contextName,contextID);

    WindowListener l = new WindowAdapter() 
        {
        public void windowClosing(WindowEvent e) { System.exit(0);}
        };
    frame.addWindowListener(l);

    frame.setSize(650,350);
    frame.setVisible(true);
    
    }
    
}
